package com.roc.web.action.change32and43;

import java.io.File;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.LinkedHashMap;
import java.util.Map;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.InterceptorRef;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.roc.newPage.IPageService;
import com.roc.page.BaseSearchCondition;
import com.roc.page.PaginationBaseAction;
import com.roc.pojo.Operators;
import com.roc.service.IExcelService;
import com.roc.service.ServiceException;
import com.roc.service.change32and43.Change32And43Service;
import com.roc.util.Constance;
import com.roc.util.constant.Constant;
import com.roc.util.constant.Role;
import com.roc.web.action.reviseMerinfo.ReviseMerinfoAction;
import com.roc.web.action.reviseMerinfo.ReviseMerinfoSearchCondition;

@Component
@Namespace("/change32And43")
@SuppressWarnings("serial")
@Scope(BeanDefinition.SCOPE_PROTOTYPE)
public class Change32And43Action extends PaginationBaseAction{
	private Logger logger = LogManager.getLogger(ReviseMerinfoAction.class);
	private ReviseMerinfoSearchCondition searchCondition;
	
	private File file;
	
	private InputStream excelFile;
    @Autowired
	private Change32And43Service change32And43Service;
    @Autowired
    private IPageService pageService;
    @Autowired
    private IExcelService excelService;
    @Autowired
	IPageService pagenitionService;
    
/**
 * 初始化页面
 * @return
 * @throws Exception
 */
    @Action(value = "init", results = { @Result(name = "success", location = "/pages/change/change_32_43.jsp"),
			@Result(name = "error", location = "/error.jsp")
    })	
	public String init() throws Exception{
		Operators op=(Operators)getSession().getAttribute(Constant.SESSION_LOGIN_MERCHANT);
		if(op.getRoleType().equals(Role.RISK_CONTROL_LEAD.getCode()) || op.getRoleType().equals(Role.RISK_CONTROL_OPERATORS.getCode())|| op.getRoleType().equals(Role.SYSTEM.getCode()))
		{
			return SUCCESS;
		}
		addActionError("无权限访问!!!");
		return ERROR;	
	}
//			return SUCCESS;  
//    }
    /**
	 * 批量导入初始化页面
	 * @return
	 */
    @Action(value = "importReviseJsp", results = { @Result(name = "success", location = "/pages/reviseMerinfo/importReviseMerinfo.jsp")
    })	
	public String changeList()throws Exception
	{
		return SUCCESS;
	}
  
	
	/**
	 * 批量导入商户号更改32域
	 * @Title: change32Lists
	 * @Description: TODO
	 * @param @return    
	 * @return String    
	 * @throws
	 */
	@Action(value = "importChange32List", interceptorRefs = {
			@InterceptorRef(value = "token"),
			@InterceptorRef(value = "rocDefaultStack", params = {
					"fileUpload.allowedExtensions", "xlsx",
					"fileUpload.maximumSize", "100000000" }) }, results = {
			@Result(name = "success",location = "/pages/merinfo/batchMerFileImport.jsp"),
			@Result(name = "error", location = "/error.jsp"),
			@Result(name = "invalid.token", location = "/error.jsp")})
	public String importChange32List()
	{
		Operators logingMerchant=(Operators)getSession().getAttribute(Constant.SESSION_LOGIN_MERCHANT);
		try {
			Map<String, Object> resultMap = new LinkedHashMap<String, Object>();
			resultMap=change32And43Service.importChange32List(file, logingMerchant);
			
			if(resultMap.size()>0){
				addActionMessage((String) resultMap.get("result"));
				addActionError((String) resultMap.get("error"));
			}
		} catch (ServiceException e) {
			e.printStackTrace();
			addActionError("更新32域失败~");
		}
		return SUCCESS;		
	}
	/**
	 * 批量导入商户号更改43域
	 * @Title: change32Lists
	 * @Description: TODO
	 * @param @return    
	 * @return String    
	 * @throws
	 */
	@Action(value = "importChange43List", interceptorRefs = {
			@InterceptorRef(value = "token"),
			@InterceptorRef(value = "rocDefaultStack", params = {
					"fileUpload.allowedExtensions", "xlsx",
					"fileUpload.maximumSize", "100000000" }) }, results = {
			@Result(name = "success",location = "/pages/merinfo/batchMerFileImport.jsp"),
			@Result(name = "error", location = "/error.jsp"),
			@Result(name = "invalid.token", location = "/error.jsp")})
	public String importChange43List()
	{
		Operators logingMerchant=(Operators)getSession().getAttribute(Constant.SESSION_LOGIN_MERCHANT);
		try {
			Map<String, Object> resultMap = new LinkedHashMap<String, Object>();
			resultMap=change32And43Service.importChange43List(file, logingMerchant);
			
			System.out.println(resultMap.size()+"  "+resultMap.get("result"));
			
			if(resultMap.size()>0){
				addActionMessage((String) resultMap.get("result"));
				addActionError((String) resultMap.get("error"));
			}
		} catch (ServiceException e) {
			e.printStackTrace();
			addActionError("更新43域失败~");
		}
		return SUCCESS;		
	}
	
	

	@Override
	protected BaseSearchCondition getSearchConditionFromSubClass()
			throws Exception {

		return searchCondition;
	}

	@Override
	protected void setSearchConditionToSubClass(
			BaseSearchCondition searchCondition) throws Exception {

		this.setSearchConditionToSubClass(searchCondition);
	}

	@Override
	protected void setSearchToConditionSession(
			BaseSearchCondition searchCondition) {

		getSession().setAttribute(SEARCH_SESSION_REVISE_MERINFO_QUERY, searchCondition);
	}

	@Override
	protected BaseSearchCondition getSearchConditionFromSession() {

		return (BaseSearchCondition) getSession().getAttribute(SEARCH_SESSION_REVISE_MERINFO_QUERY);
	}

	@Override
	protected String getInitPageSize() {

		return Constance.PAGE_MAX_SIZE+"";
	}
	


	public ReviseMerinfoSearchCondition getSearchCondition() {
		return searchCondition;
	}


	public void setSearchCondition(ReviseMerinfoSearchCondition searchCondition) {
		this.searchCondition = searchCondition;
	}


	public File getFile() {
		return file;
	}


	public void setFile(File file) {
		this.file = file;
	}
	public InputStream getExcelFile() {
		return excelFile;
	}
	public void setExcelFile(InputStream excelFile) {
		this.excelFile = excelFile;
	}
	public String getExcelName() {
		String excelName="商户整改数据表"+Constance.formateDate();
		 
			try {
				excelName = new String(excelName.getBytes(),"ISO8859-1");
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
				logger.info("生成excel名称失败了！");
			}
		 
		return  excelName;
		
	}
}
